<template name="sendcar">
	<view class="mypage">
		<view class="cu-bar search bg-white">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input v-model="keyword" type="text" placeholder="输入申请人" />
			</view>
			<view class="action" @tap="handelSearch">
				<button class="cu-btn bg-blue shadow-blur round">搜索</button>
			</view>
		</view>

		<view class="card-list-wrapper">
			<view class="card-list-row radius shadow-warp bg-white margin-top" @click="openDetail(item)"
				v-for="(item,index) in planList" :key="index">
				<view class="flex-just-between bottom-border top-wrap">
					<text class="text-blue text-bold">单号：{{ item.CODE }}</text>
					<view class="cu-capsule radius">
						<view class="cu-tag bg-blue sm">
							<text class="cuIcon-medal"></text>
						</view>
						<view class="cu-tag line-brown sm">{{ index+1}}</view>
					</view>
				</view>

				<view class="card-list-row-1">
					<!-- <text class="cuIcon-title text-red"></text> -->
					<text>类型：{{ item.SCHEMENAME || '' }}</text>
				</view>

				<view class="card-list-row-1">
					<!-- <text class="cuIcon-title text-red"></text> -->
					<text>状态：{{ item.ISFINISH == '1'?'已审核':'审批中' }}</text>
				</view>

				<view class="card-list-row-1">
					<!-- <text class="cuIcon-title text-red"></text> CREATEDATE -->
					<!-- <text>日期：{{ item.CreateDate?item.CreateDate.replace('T',' '):'' }}</text> -->
					<text>日期：{{ item.CREATEDATE || '' }}</text>
				</view>

				<view class="card-list-row-1">
					<!-- <text class="cuIcon-title text-red"></text>  CreateUserName -->
					<text>提交人：{{ item.CREATEUSERNAME }}</text>

				</view>

				<view class="flex-just-between top-border margin-top btn-wrap" v-if="type==0">
					<button class="cu-btn round lines-yellow" @click.stop="appReject(3,index)">驳回</button>
					<button class="cu-btn round lines-blue" @click.stop="appResult(1,index)">同意</button>
				</view>

				<view class="flex-just-center top-border margin-top btn-wrap" v-if="type==2">
					<button class="cu-btn round lines-blue" @click.stop="submitData(item)">提交</button>
				</view>
			</view>
		</view>
		<modal title="请输入驳回原因" placeholder="请输入驳回原因" :isShow="isShowModal" @cancelModal="cancelModal"
			@okModal="okModal"></modal>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex";
	import modal from "@/components/modal/modal.vue";
	import api from "@/common/api.js";
	import utils from "@/common/util.js";
	let query = {
			page: 1,
			rows: "10",
			sidx: "",
			sord: "",
			queryJson: {},
			userId: "",
			account: ""
		},
		_self,
		page = 1,
		total = 1,
		pageTitle = "列表";
	let queryJson = {
		Code: "",
		ISFINISH: 0
	};
	let chooseItem = "";
	let saveData = {
		FlowModel: "",
		WfSchemeInfoId: "",
		BillNo: "",
		FrmData: "",
		VerificationData: "",
		WfProcessId: "",
		UserInfo: {
			UserId: ""
		},
		ModuleType: "PP_PLAN",
		Opinion: ""
	};
	let approvalRes = {
		NodeRejectStep: "",
		VerificationOpinion: "",
		VerificationFinally: "1"
	};
	
	export default {
		computed: mapState(["userInfo", "baseUrl"]),
		data() {
			return {
				title: "列表",
				type: 0,
				planList: [],
				keyword: "",
				isShowModal: false
			};
		},
		components: {
			modal
		},
		
		onNavigationBarButtonTap(e) {
			this.listenOnce()
			uni.navigateTo({
				url: '/pages/public/query?type=menu1013' 
			})
		},
		
		onLoad(params) {
			_self = this;
			pageTitle = params.n;
			_self.type = params.type;
			console.log(params);
			queryJson ={}
			queryJson.StartTime = "";
			queryJson.EndTime = "";
			queryJson.ISFINISH = params.type == 2 ? 4 : params.type;
			queryJson.Code = "";
			getApp().globalData.queryJsonLS = null
			
			query.userId = _self.userInfo.UserId;
			query.account = _self.userInfo.Account;
			page = 1;
			total = 1;
			_self.getNewsList();
		},
		onShow() {
			uni.$once("updateApproval", data => {
				queryJson.StartTime = data.s;
				queryJson.EndTime = data.e;
				page = 1;
				total = 1;
				_self.getNewsList();
			});
		},

		//下拉刷新
		onPullDownRefresh: function() {
			page = 1;
			total = 1;
			_self.planList = [];
			this.getNewsList();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
			utils.tips("数据已刷新");
		},
		// 加载更多
		onReachBottom: function() {
			this.getNewsList();
		},
		methods: {
			...mapMutations(["setCheckOrder"]),
			openDetail: function(d) {
				let ttt22 = '' // _self.getAuditType(JSON.parse(ttt).Flow.nodes);
				if (_self.type == 2) {
					return;
				}
				uni.navigateTo({
					url: "/pages/tab04/approval_detail/approval_detail?dbType=" +
						ttt22 +
						"&doType=" +
						_self.type +
						"&item=" +
						JSON.stringify(d)
				});
			},
			// 初始监听
			listenOnce() {
				uni.$off('reload')
				uni.$once("reload", function(data) {
					queryJson= { ...queryJson , ...data }
					getApp().globalData.queryJsonLS = queryJson
					_self.handelSearch()
				});
			},
			handelSearch() {
				// console.log(_self.keyword);
				// queryJson.Code = _self.keyword;
				queryJson.CREATEUSERNAME = _self.keyword;
				// _self.keyword = "";
				page = 1;
				total = 1;
				_self.planList = []
				this.getNewsList();
			},
			appResult(e, i) {
				let t = "同意";
				approvalRes.NodeRejectStep = "";
				approvalRes.VerificationFinally = e.toString();
				approvalRes.VerificationOpinion = "";
				let item = _self.planList[i];
				uni.showModal({
					title: "提示",
					content: "您确定要" + t + "吗？",
					success: function(res) {
						res.confirm ? _self.submitApproval(item) : null;
					}
				});
			},

			getNewsList() {
				if (page > total) {
					return uni.showToast({
						icon: "none",
						title: "没有数据了。"
					});
				}
				query.page = page;
				query.queryJson = JSON.stringify(queryJson);
				let URL = api.apiPublic.approvalList;
				// console.log(URL);
				// console.log(query);
				this.axios({
					url: URL,
					data: query,
					isLoading: 1
				}).then(res => {
					let {
						Message,
						Result
					} = res;
					console.log(Result);
					let tnt = Result.approval.rows;
					let records = Result.approval.records;
					total = Result.approval.total;
					if (page == 1) {
						// _self.planList = [];
						// _self.planList = tnt;
						utils.setPageTitle(pageTitle + "列表(" + records + ")");
						// _self.title = originalTitle + '('+res.data.Result.records +')'
					}
					_self.planList = [..._self.planList, ...tnt];
					page++;

				})
			},

			submitApproval(d) {
				console.log(d);
				let saveData = {
					"BillNo": d.CODE ,
					"WfProcessId": d.CODE ,
					"VerificationData": {
						"VerificationFinally": approvalRes.VerificationFinally,
						"NodeRejectStep": "",
						"VerificationOpinion": approvalRes.VerificationOpinion || '' ,
					},
					"Code": d.CONTENT , 
					"category": d.TABLENAME || '' ,
					"ModuleID": d.CONTENT
				}
				let URL = '/api/APPCommon/AuditFlow' 
				console.log(saveData);
				this.axios({
					url: URL,
					data: saveData ,
					isLoading: 1
				}).then(res => {
					console.log(res);
					utils.tips("审批成功");
					page = 1;
					total = 1;
					setTimeout(res => {
						_self.getNewsList();
					}, 1600)
				}).catch(err => {
					console.log(err);
				})
			},

			// 审批节点截取类型
			getAuditType(data) {
				let res = "";
				data.forEach(item => {
					if (item.setInfo) return (res = item.setInfo.NodeTable);
				});
				// console.log(res);
				return res;
			},

			// 驳回事件
			appReject(e, i) {
				approvalRes.NodeRejectStep = "FlowPanel_node_1";
				approvalRes.VerificationFinally = e.toString();
				chooseItem = _self.planList[i];
				this.isShowModal = true;
			},
			// 驳回时弹出对话框
			cancelModal() {
				this.isShowModal = false;
			},
			okModal(val) {
				this.cancelModal();
				console.log(val);
				saveData.Opinion = val;
				approvalRes.VerificationOpinion = val;
				this.submitApproval(chooseItem);
			},

			// 重新审批，提交进入审批流
			submitData(item) {
				// console.log(888);
				console.log(JSON.stringify(item));
				uni.showModal({
					title: "提示",
					content: "您确定要提交已被驳回的订单？提交后将重新进入审批流。",
					success: function(res) {
						res.confirm ? _self.submitDataAjax(item) : null;
					}
				});
			},

			submitDataAjax(data) {
				let URL = _self.baseUrl + "/api/APPCommon/ApprovingFlow";
				URL +=
					`?wfSchemeInfoId=${data.CONTENT}&ModuleType=${data.PREVIOUSADUITNAME}&account=${_self.userInfo.UserId}&keyValue=${data.Code}`;
				console.log(URL)
				_self.axios({
						url: URL
					})
					.then(res => {
						utils.tips("审批成功");
						page = 1;
						total = 1;
						_self.getNewsList();
					});
			}
		}
	};
</script>

<style scoped>
	.card-list-wrapper {
		width: 710upx;
		height: 100upx;
		margin: 0 auto;
	}

	.card-list-row {
		padding: 20upx;
	}

	.card-list-row-1 {
		padding: 10upx 0;
	}

	.top-border {
		border-top: 1upx solid rgba(0, 0, 0, 0.1);
	}

	.top-wrap {
		padding-bottom: 20upx;
		margin-bottom: 20upx;
	}

	.bottom-border {
		border-bottom: 1upx solid rgba(0, 0, 0, 0.1);
	}

	.btn-wrap {
		padding-top: 20upx;
	}
</style>
